/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rainbow.xml;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.rainbow.udo.IllegalAccessRuntimeException;
import org.rainbow.udo.InstantiationRuntimeException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author hironaga
 */
class XmlNativeHandlerImpl extends XmlNativeHandler{

    private XmlHandleHelper helper;

    private XmlCommonHandler commonHandler = XmlCommonHandler.getInstance();

    XmlNativeHandlerImpl() {
        this.helper = new XmlHandleHelperImpl();
    }

    <T> T readObject(File file,String xpath, Class<T> cls)
                                                             throws IOException{
        Document document = commonHandler.getDocument(file);

        T obj;
        try {
            obj = helper.getElementData(document, xpath, cls);
        } catch (InstantiationException ex) {
            throw new InstantiationRuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new IllegalAccessRuntimeException(ex);
        }

        return obj;

    }

    <T> List<T> readList(File file,String xpath, Class<T> cls)
                                                            throws IOException{
        Document doc = commonHandler.getDocument(file);

        List<T> objList;
        try {
            objList = helper.getElementDataList(doc, xpath, cls);
        } catch (InstantiationException ex) {
            throw new InstantiationRuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new IllegalAccessRuntimeException(ex);
        }

        return objList;
    }

    void writeObject(File file, String xPath, Object obj) throws IOException {
        writeObject(xPath, obj, file, commonHandler.DEFAULT_VERSION, commonHandler.DEFAULT_ENCODING);
    }

    void writeList(File file, String xPath, List<?> list) throws IOException {
        writeList(xPath, list, file, commonHandler.DEFAULT_VERSION, commonHandler.DEFAULT_ENCODING);
    }

    void appendObject(File file, String xPath, Object obj) throws IOException {
        appendObject(xPath, obj, file, commonHandler.DEFAULT_VERSION, commonHandler.DEFAULT_ENCODING);
    }

    void appendList(File file, String xPath, List<?> list) throws IOException {
        appendList(xPath, list, file, commonHandler.DEFAULT_VERSION, commonHandler.DEFAULT_ENCODING);
    }

    void overwriteObject(File file,String xPath, Object obj) throws IOException {
        overwriteObject(xPath, obj, file, commonHandler.DEFAULT_VERSION, commonHandler.DEFAULT_ENCODING);
    }

    protected void writeObject(String xPath, Object obj, File file, String version, String encoding)
                                                        throws IOException {

        //Set Document
        Document doc = commonHandler.getNewDocument();

        //Add root & parent node
        Node parentElement = commonHandler.createParerntElement(doc, xPath);

        //delegate heler class
        helper.createElement(obj, doc, xPath, parentElement, 0);

        //Write2XML
        commonHandler.writeFile(file, doc, encoding);

    }

    protected void writeList(String xPath, List<?> list, File file, String version, String encoding)
                                                            throws IOException {

        //Set Document
        Document doc = commonHandler.getNewDocument();

        //Add root & parent node
        Node parentElement = commonHandler.createParerntElement(doc, xPath);

        //delegate heler class
        for(int i=0; i<list.size(); i++){
            //set node index
            helper.createElement(list.get(i), doc, xPath, parentElement, i);
        }

        //Write2XML
        commonHandler.writeFile(file, doc, encoding);

    }

   protected void appendObject(String xPath, Object obj, File file, String version, String encoding)
                                                            throws IOException {

        //Set Document
        Document doc = commonHandler.getDocument(file);

        //Count nodelist
        NodeList nodeList = commonHandler.getNodeList(doc, xPath);
        commonHandler.validateXpathForAppend(xPath);
        int lastNodeListIndex = nodeList.getLength();

        //Get parent node
        Node parentElement = commonHandler.getParentElement(doc, xPath);

        //delegate heler class
        helper.createElement(obj, doc, xPath, parentElement, lastNodeListIndex);

        //Write2XML
        commonHandler.writeFile(file, doc, encoding);

    }

   protected void appendList(String xPath, List<?> list, File file, String version, String encoding)
                                                            throws IOException {

        //Set Document
        Document doc = commonHandler.getDocument(file);

        //Count nodelist
        NodeList nodeList = commonHandler.getNodeList(doc, xPath);
        commonHandler.validateXpathForAppend(xPath);
        int lastNodeListIndex = nodeList.getLength();

        //Get parent node
        Node parentElement = commonHandler.getParentElement(doc, xPath);

        //delegate heler class
        for(int i=0; i<list.size(); i++){
            helper.createElement(list.get(i), doc, xPath, parentElement, lastNodeListIndex + i);
        }

        //Write2XML
        commonHandler.writeFile(file, doc, encoding);

    }

   protected void overwriteObject(String xPath, Object obj, File file, String version, String encoding)
                                                            throws IOException {

        //Set Document
        Document doc = commonHandler.getDocument(file);

        //Count nodelist
        NodeList nodeList = commonHandler.getNodeList(doc, xPath);
        commonHandler.validateXpathForAppend(xPath);
        //TODO ?S?
        int lastNodeListIndex = nodeList.getLength();

        //Get parent node
        Node parentElement = commonHandler.getParentElement(doc, xPath);

        //delegate heler class
        helper.overwriteElement(obj, doc, xPath, parentElement, lastNodeListIndex);

        //Write2XML
        commonHandler.writeFile(file, doc, encoding);

    }
   
}
